﻿using System.Drawing;
using System.Drawing.Imaging;
using System.Text;

namespace _20_C_中二进制数据与图片相互转换
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "C#中二进制数据与图片之间的转换";

            byte[] bytes = ImageToBinary();

            Console.WriteLine(Encoding.UTF8.GetString(bytes));

            BinaryToImage();

            Console.ReadKey();
        }
        static void BinaryToImage()
        {
            byte[] bytes = ImageToBinary();
            Image image;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                image = Image.FromStream(ms);

            }
            image.Save("转换后的图片.png",ImageFormat.Png);
        }

        static byte[] ImageToBinary()
        {

            byte[] bytes;
            string path = "2.png";

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                bytes=new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
            }
            return bytes;
        }
    }
}
